<?php

class Dao_CategoryDAO extends Dao_AbstractDAO {
	
	public function __construct($db = null) {
		parent::__construct($db);
		$this->tableName = "category";
		$this->key = "id";
		$this->classModel = "Model_Category";
	}
	
	public function getCategory($page = null, $number = null) {
		$query = $this->createSQLGetCategory();
		$query = $this->appendLimit($query, $page, $number);
		return $this->getList($query);
	}
	
	private function createSQLGetCategory() {
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE T.delete_flg = 0 ";
		$query .= " ORDER BY T.order_index ASC ";
		return $query;
	}
	
	public function getCategoryWithoutDelete($start = null, $number = null) {
		$query = $this->createSQLGetCategoryWithoutDelete($start, $number);
		$query = $this->appendLimit($query, $start, $number);
		return $this->getList($query);
	}
	
	public function countCategoryWithoutDelete($start = null, $number = null) {
		$query = $this->createSQLCountCategoryWithoutDelete($start, $number);
		$res = $this->query($query);
		if (count($res) > 0) {
			return $res[0]['CNT'];
		} else { 
			return 0;
		}
	}
	
	private function createSQLGetCategoryWithoutDelete($start = null, $number = null) {
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " ORDER BY T.order_index ASC ";
		return $query;
	}
	
	private function createSQLCountCategoryWithoutDelete($start = null, $number = null) {
		$query = " SELECT COUNT(*) AS CNT FROM $this->tableName T ";
		return $query;
	}
	
	/**
	 * 
	 * @param string $id
	 * @return Model_Category
	 */
	public function getCategorybyId($id) {
		$query = $this->createSQLGetCategoryById($id);
		return $this->getOneObject($query);
	}
	
	private function createSQLGetCategoryById($id) {
		$id = $this->removeEscape($id);
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE T.id = '" . $id . "'" ;
		return $query;
	}
	
	
	/**
	 * 
	 * @param Model_Category $category
	 * @return boolean
	 */
	public function insertCategory(Model_Category $category){
		if ($category === null) {
			return false;
		}
		
		$this->startTransaction();
		$success = $this->insertModel($category);
		
		if ($success) {
			$this->commitTransaction();
			return true;
		} else {
			$this->rollBackTransaction();
			return false;
		}
		
		return $success;
	}
	
	
	/**
	 * updateCategory
	 * @param Model_Category $category
	 * @return boolean|Ambigous <boolean, mixed>
	 */
	public function updateCategory(Model_Category $category){
		if ($category === null) {
			return false;
		}
		$success = false;
		
		$sql = $this->startTransaction();
		
		$success = $this->updateModelById($category);
		
		if ($success) {
			$this->commitTransaction();
		} else {
			$this->rollBackTransaction();
		}
		
		return $success;
	}
	
	public function insertOrUpdate(Model_Category $category) {
		$success = false;
		if ($category->getId() !== null) {
			
			$success = $this->updateCategory($category);
		} else {
			$success = $this->insertCategory($category);
		}
		return $success;
	}
}